Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var parent_id = 0;//默认类别为父类别
	var parent_level = 0;
	var c_node = null;
	var categoryTree = new Ext.tree.TreePanel({
		title:'业务',
		region:'west',
		frame:true,
		width:200,
		split:true,
		collapsible:true,
		rootVisible : false,
		autoScroll : true,
		
		root:new Ext.tree.AsyncTreeNode({
			id : '0',
			text : '类别'
		}),
		loader: new Ext.tree.TreeLoader({
			url:'/wsd/admin/category_categoryTreeData.action'
    	})
	});
	categoryTree.expandAll();
		
	
	var categoryField = new Ext.form.TextField({
		fieldLabel:'父类别',
		value:'默认类别',
		readOnly:true,
		width:200
	})
	var addCategoryArea = new Ext.form.TextArea({
		
		fieldLabel:'新增类别',
		autoScroll:true,
		width:200,
		height:200
	})
	var label = new Ext.form.Label({
		text:'(一行一个类别)'
	});
	var addBtn = new Ext.Button({
		text:'添加类别',
		handler:function(){
			if(parent_id == 0 && parent_level == 0){
				Ext.Msg.alert('系统信息','请先选择类别');
				return;
			}
			var categories = new Array();
			var addItem = addCategoryArea.getValue().split('\n');
			for(var i = 0; i < addItem.length; i++){
				var s = addItem[i].trim();
				if( s == ''){
					continue;
				}
				categories.push(s);
			}
			if(categories.length == 0){
				Ext.Msg.alert("系统信息","添加内容不能为空");
				addCategoryArea.focus();
				return;
			}
			//向后台发送数据
			send(categories);
		}
	})
	var form = new Ext.form.FormPanel({
		region:'center',
		frame:'true',
		items:[categoryField,
			{
				layout:'column',
				items:[
					
				{
					layout:'form',
					items:[addCategoryArea]
				},label]
			},addBtn
		]
	})
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [categoryTree,form]
	});
	categoryTree.on("click",function(node,e){
		addCategoryArea.setValue('');
		parent_id = node.attributes.id;
		parent_level = node.attributes.level;
		categoryField.setValue(node.attributes.text);
	})
	/**
	 * 向后台提交新添加的类别
	 */
	function send(categories){
		Ext.Msg.wait("正在添加……");
		Ext.Ajax.request({
			url:'/wsd/admin/category_addCategories.action',
			params:{
				cNames:categories,
				'parent.CLevel':parent_level,	
				'parent.CId':parent_id
			},
			
			success:function(response){
				var result = Ext.util.JSON.decode(response.responseText).result;
				if(!result.addResult){
					Ext.Msg.alert("系统信息","添加失败");
					return;
				}
				var categories = result.categories;
				var pNode = categoryTree.getNodeById(parent_id);
				var len = categories.length;
				pNode.attributes.leaf = false;
				for(var i = 0; i < len; i++){
					var categorie = categories[i];
					var node = new Ext.tree.TreeNode({
						id:categorie.CId,
						text:categorie.CName,
						leaf:true,
						level:categorie.CLevel
				})
					pNode.appendChild(node)
				}
				categoryTree.expandAll();
				Ext.Msg.hide();
				Ext.Msg.alert("系统信息","添加" + result.addSize + "条记录");
			},
			failure:function(response){
				Ext.Msg.alert('系统消息','添加失败');
			}
		})
		
		
	}
	
});